import { cloneDeep,uniqBy } from 'lodash'
import { parse } from 'qs'
import { message } from 'antd'
import { Link, hashHistory } from 'dva/router'
import {
  updateLifeService,
  createLifeService,
  getLifeService,
  getCategoryList
} from './lifeServiceEditServ'

const defultState = {
  actionType: '',
  detail: {},
  images:[],
  categoryList:[],
  query: {
    name: '',
    mobile: ''
  }
}

const tmpModule = {
  state: cloneDeep(defultState),
  subscriptions: {
    setup ({dispatch, history}) {
      history.listen(({pathname, query}) => {
        if ('' + tmpModule.pathname === '' + pathname) {
          dispatch({ type: 'updateStore', payload: cloneDeep(defultState) });
          // 执行业务操作
          let {id, actionType} = query
          if(id){
            dispatch({type: 'getDetail', payload: {id}})
          }
          dispatch({type: 'updateStore', payload: {actionType}})
          dispatch({type: 'fetchCategoryList', payload: {}})
        }
      })
    }
  },
  
  effects: {    
    * createLifeService ({payload}, {put, select, call}) {
        let {detail} = payload
        console.log(detail)
        let {images} = yield select(state => state[tmpModule.namespace])
        if(images.length == 0){
          message.error("封面不能为空")
          return
        }
        detail.cover = images.length > 0 ? images[0]:""
        let {data, success, msg} = yield call(createLifeService, detail)
        if (success) {
          message.success('新建服务成功', () => {
              defultState.biz.refreshListDatas(true)
          })
        
        } else {
          message.error(msg)
        }
    },
    * fetchCategoryList({payload}, {put, select, call}){
      let {rows, success, msg} = yield call(getCategoryList, payload)
      if (success) {
        let categoryList = rows
        console.log("xxxxxx",categoryList)
        yield put({type: 'updateStore', payload: {categoryList}})
        // dispatch({type: 'updateStore', payload: {categoryList}})
      }
    
    },
    * updateLifeService ({payload}, {put, select, call}) {
        let {detail} = payload
        console.log(detail)
        let {images} = yield select(state => state[tmpModule.namespace])
        if(images.length == 0){
          message.error("封面不能为空")
          return
        }
        detail.cover = images.length > 0 ? images[0]:""
        let {data, success, msg} = yield call(updateLifeService, detail)
        if (success) {
          message.success('修改服务成功', () => {
              defultState.biz.refreshListDatas(true)
          })
        
        } else {
          message.error(msg)
        }
    },

    * getDetail ({payload}, {put, select, call}) {
      try {
        
        let {data, success} = yield call(getLifeService, payload)
        let detail = {}
        if (success) {
          detail = data
          let images = [detail.cover]
          yield put({type: 'updateStore', payload: {detail, images}})
        }
      } catch (e) {
        console.error(e)
      }
    },
  },
  
  reducers: {
    updateStore (preState, action) {
      return {...preState, ...action.payload}
    }
  }
}

export default tmpModule
